package com.zgs.mall.order.web;

import com.zgs.common.constant.LoginConstant;
import com.zgs.mall.order.entity.OrderEntity;
import com.zgs.mall.order.service.OrderService;
import com.zgs.mall.order.vo.MemberRespVo;
import com.zgs.mall.order.vo.OrdeerConfirmVo;
import com.zgs.mall.order.vo.SubmitOrderResponse;
import com.zgs.mall.order.vo.SubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
    @Autowired
    OrderService orderService;

    /**
     * 订单确认页
     * @param model
     * @return
     */
    @GetMapping("toTrade")
    public  String toTrade(Model model) throws ExecutionException, InterruptedException {

        OrdeerConfirmVo confirmVo= orderService.confirmOrder();
        model.addAttribute("orderConfirmData",confirmVo);
        return "confim";
    }
    //提交订单
    @GetMapping("submitOrder")
    public  String submitOrder(SubmitVo submitVo, Model model) throws ExecutionException, InterruptedException {
        //下单创建订单，验证token，验价格，锁定库存

        SubmitOrderResponse submitOrderResponse =orderService.submitOrder(submitVo);
        //下单成功跳转到支付选择页
        Integer code = submitOrderResponse.getCode();
        if (code==0){
            OrderEntity order = submitOrderResponse.getOrder();
            return "pay";
        }else{
            //失败跳转到订单确认页
            return "redirect:http://order.gulimall.com/toTrade";
        }

    }
}
